<?php
/**
 * menu表模型
 *
 * @author
 * @version
 */
class MenuModel extends Model
{

	//protected $tableName = 'menu';	// 表名
	//protected $trueTableName = 'ct_menu';		// 完整表名
	//protected $dbName = 'ct';		// 数据库名
	//protected $fields = array();  // 字段列表

	// 自动验证
	protected $_validate = array(
		array("name", "require", "菜单名称不为空"),
		array('url', 'require', "菜单URL不为空"),
	);

	/**
	 * @var Tree
	 */
	private $Tree = NULL;
	private $menu = NULL;

	/**
	 * 模型预初始化
	 *
	 * @return NULL
	 */
	protected function _initialize()
	{
		import('@.Helper.Tree');
		$this->Tree = new Tree(C('DB_PREFIX') . 'menu', 'menuid');
		$this->menu = table('menu');
	}

	/**
	 * 获取指定菜单ID的所有子菜单ID
	 *
	 * @param int $menuid
	 * @return mixed
	 */
	public function getChild($menuid = NULL)
	{
		return $this->Tree->getChild($menuid, '`menuid`, `parentids`, `childids`, `name`, `url`, `sort`');
	}


	/**
	 * 添加菜单节点
	 *
	 * @param mixed|string $data
	 * @return mixed
	 */
	public function add($data)
	{
		return $this->Tree->set($data);
	}

	/**
	 * @param $menuid
	 * @param $data
	 * @return mixed
	 */
	public function edit($menuid, $data)
	{
		$data['menuid'] = $menuid;
		return $this->Tree->set($data);
	}

	/**
	 * 修复菜单节点的父子级关系
	 */
	public function repair()
	{
		@set_time_limit(600);
		if (is_array($this->menu)) {
			$menuids = array_keys($this->menu);
			foreach ($menuids as $menuid) {
				if ($menuid == 0) continue;
				$this->menu[$menuid]['parentids'] = $parentids[$menuid] = $this->getParentids($menuid);
			}

			foreach ($menuids as $menuid) {
				if ($menuid == 0) continue;
				$childids = $this->getChildids($menuid);
				$this->where('menuid='.$menuid)->save(array('parentids' => $parentids[$menuid], 'childids' => $childids));
			}
		}
		return true;
	}

	/**
	 * 获取指定菜单节点的父级关系
	 *
	 * @param $menuid
	 * @param string $parentids
	 * @param int $n
	 * @return bool
	 */
	function getParentids($menuid, $parentids = '', $n = 1)
	{
		if (!is_array($this->menu) || !isset($this->menu[$menuid]))
			return false;
		$parentid = $this->menu[$menuid]['parentid'];
		if ($parentid && $parentid != $menuid) {
			$parentids = $parentids ? $parentid . ',' . $parentids : $parentid;
			$parentids = $this->getParentids($parentid, $parentids, ++$n);
		}
		else {
			if (!$parentids) $parentids = null;
			$this->menu[$menuid]['parentids'] = $parentids;
		}
		return $parentids;
	}

	/**
	 * 获取指定菜单节点的子级关系
	 *
	 * @param $menuid
	 * @return array|null|string
	 */
	function getChildids($menuid)
	{
		$childids = array();
		if (is_array($this->menu)) {
			foreach ($this->menu as $id => $cat) {
				if ($cat['parentid'] && $id != $menuid) {
					$parentids = explode(',', $cat['parentids']);
					if (in_array($menuid, $parentids)) $childids[] = $id;
				}
			}
		}
		$childids = implode(',', $childids);
		if (!$childids) $childids = null;
		return $childids;
	}

	/**
	 * 删除指定菜单节点
	 *
	 * @param array|mixed $menuid
	 * @return array|mixed
	 */
	public function delete($menuid)
	{
		return is_numeric($menuid) ? $this->Tree->rm($menuid) : array_map(array($this->Tree, 'rm'), explode(',', $menuid));
	}
}

?>